﻿Imports DAL
Public Class PessoaBLL
    Dim ObjDAL As New PessoaDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Nome As String
    Property Telefone As String
    Property Celular As String
    Property Email As String
    Property Cep As String
    Property Endereco As String
    Property Cidade As String
    Property Uf As String
    Property Bairro As String
    Property Estado As String
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, nome As String, telefone As String, celular As String, email As String, cep As String, endereco As String, cidade As String, uf As String, bairro As String)
        Me.Codigo = codigo
        Me.Nome = nome
        Me.Telefone = telefone
        Me.Celular = celular
        Me.Email = email
        Me.Cep = cep
        Me.Endereco = endereco
        Me.Cidade = cidade
        Me.Uf = uf
        Me.Bairro = bairro
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir/Alterar"
    Function InserirAlterar(usuario As String) As Boolean
        Try
            ObjDAL = New PessoaDAL
            If (Trim(Nome) <> "" And Trim(Cidade) <> "" And Trim(Bairro) <> "" And Trim(Endereco) <> "" And Trim(Cep) <> "") Then
                If (ObjDAL.ValidaReplica(Me.Codigo, Me.Nome)) Then
                    Me.Codigo = ObjDAL.InserirAlterar(Me.Codigo, Me.Nome, Me.Telefone, Me.Celular, Me.Email, Me.Endereco, Me.Cep, Me.Cidade, Me.Uf, Me.Bairro, usuario)
                Else : Throw New Exception("Unidade já cadastrada")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        Dim ret As Integer
        Try
            ObjDAL = New PessoaDAL
            If (ObjDAL.ValidaExcluir(Me.Codigo)) Then
                ret = ObjDAL.Excluir(Codigo)
            Else
                Throw New Exception("Esta Pessoa não pode ser excluida, verifique antes a ocorrência da mesma no(s) formulário(s): Coleta")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret = -1) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New PessoaDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New PessoaDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("pes_codigo").ToString)
                Me.Nome = tab.Rows(0)("pes_nome").ToString
                Me.Telefone = tab.Rows(0)("pes_telefone").ToString
                Me.Celular = tab.Rows(0)("pes_celular").ToString
                Me.Cep = Integer.Parse(tab.Rows(0)("pes_cep").ToString)
                Me.Email = tab.Rows(0)("pes_email").ToString
                Me.Endereco = tab.Rows(0)("pes_endereco").ToString
                Me.Bairro = tab.Rows(0)("pes_bairro").ToString
                Me.Estado = tab.Rows(0)("uf_descricao").ToString
                Me.Uf = tab.Rows(0)("uf_uf").ToString
                Me.Cidade = tab.Rows(0)("pes_cidade").ToString

            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Codigo = Nothing
        Me.Nome = Nothing
        Me.Telefone = Nothing
        Me.Celular = Nothing
        Me.Cep = Nothing
        Me.Email = Nothing
        Me.Endereco = Nothing
        Me.Bairro = Nothing
        Me.Uf = Nothing
        Me.Cidade = Nothing
        Me.Estado = Nothing


    End Sub
#End Region
End Class
